using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;
namespace MemoryGame
{

    public class SoundManager
    {

        public static int SOUND_TITLE = 0;
        public static int SOUND_INGAME_BG = 1;

        public static int SOUND_SELECT = 0;
        
        public static int SOUND_CLICK_CARD = 1;
        public static int SOUND_COMBOL_1 = 2;
        public static int SOUND_COMBOL_2 = 3;
        public static int SOUND_COMBOL_3 = 4;
        public static int SOUND_WIN = 5;
        public static int SOUND_LOSE = 6;
        public static int SOUND_BACK = 7;
        public static int SOUND_FAIL = 8;
        public static int SOUND_RIGHT = 9;
        public static int SOUND_SLIPALL = 10;

        static private SoundManager _instance;
        public static SoundEffect[] playerEffect;

        public static Song[] musicLoop;
        public static Song musicLoopIngame;
        public static SoundEffectInstance soundEffectInstance;

        //     public static MediaPlayer playerLoop;


        private SoundManager()
        {
        }

        /**
         * Requests the instance of the Sound Manager and creates it
         * if it does not exist.
         *
         * @return Returns the single instance of the SoundManager
         */
        static public SoundManager getInstance()
        {
            if (_instance == null)
                _instance = new SoundManager();
            return _instance;
        }

        /**
         * Initialises the storage for the sounds
         *
         * @param theContext The Application context
         */
        public static void initSounds()
        {
            playerEffect = new SoundEffect[12];
            playerEffect[SOUND_SELECT] = Game1.contentManager.Load<SoundEffect>("sound//select");
            playerEffect[SOUND_CLICK_CARD] = Game1.contentManager.Load<SoundEffect>("sound//button_click"); ;
            playerEffect[SOUND_COMBOL_1] = Game1.contentManager.Load<SoundEffect>("sound//combo_1");
            playerEffect[SOUND_COMBOL_2] = Game1.contentManager.Load<SoundEffect>("sound//combo_2");
            playerEffect[SOUND_COMBOL_3] = Game1.contentManager.Load<SoundEffect>("sound//combo_3");
            playerEffect[SOUND_WIN] = Game1.contentManager.Load<SoundEffect>("sound//game_win");
            playerEffect[SOUND_LOSE] = Game1.contentManager.Load<SoundEffect>("sound//game_over");
            playerEffect[SOUND_BACK] = Game1.contentManager.Load<SoundEffect>("sound//select");
            playerEffect[SOUND_FAIL] = Game1.contentManager.Load<SoundEffect>("sound//fail");
            playerEffect[SOUND_RIGHT] = Game1.contentManager.Load<SoundEffect>("sound//right");
            playerEffect[SOUND_SLIPALL] = Game1.contentManager.Load<SoundEffect>("sound//slipall");
            musicLoop = new Song[2];
            //  musicLoop[0] = Game1.contentManager.Load<Song>("sound//m_title");
            musicLoop[0] = Game1.contentManager.Load<Song>("sound//menu_background");
            musicLoop[1] = Game1.contentManager.Load<Song>("sound//game_background");
        }
        public static void playsoundLoop(int index, bool loop)
        {
            FrameworkDispatcher.Update();
            if (true)
                return;
            if (!Game1.isEnableSound)
                return;
            if (musicLoop[index] == null)
                return;
            try
            {

                if (MediaPlayer.GameHasControl)
                {

                    MediaPlayer.IsRepeating = loop;
                    MediaPlayer.Play(musicLoop[index]);

                }
            }
            catch (Exception e)
            {
            }

        }
        public static void pausesoundLoop(int index)
        {
            if (true)
                return;
            if (!Game1.isEnableSound)
                return;
            try
            {
                MediaPlayer.Pause();
            }
            catch (Exception e)
            {
            }
        }
        public static void stopsoundLoop(int index)
        {
            if (true)
                return;
            if (!Game1.isEnableSound)
                return;
            try
            {
                MediaPlayer.Stop();
            }
            catch (Exception e)
            {
            }
        }
        public static void loadSounds()
        {

        }

        /**
         * Plays a Sound
         *
         * @param index - The Index of the Sound to be played
         * @param speed - The Speed to play not, not currently used but included for compatibility
         */
        public static void playSound(int index)
        {
            if (!MediaPlayer.GameHasControl)
                return;
            soundEffectInstance = playerEffect[index].CreateInstance();
            soundEffectInstance.Play();
        }


        /**
         * Stop a Sound
         * @param index - index of the sound to be stopped
         */
        public static void stopSound(int index)
        {
            if (soundEffectInstance != null)
                soundEffectInstance.Stop();
        }

        /**
         * Deallocates the resources and Instance of SoundManager
         */
        public static void cleanup()
        {


        }

    }
}